package com.music.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.music.server.pojo.ListSong;
import com.music.server.pojo.SongList;
import com.music.server.mapper.SongListMapper;
import com.music.server.service.ListSongService;
import com.music.server.service.SongListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.music.server.service.SongService;
import com.music.server.vo.SongListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 歌单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-08
 */
@Service
public class SongListServiceImpl extends ServiceImpl<SongListMapper, SongList> implements SongListService {

    @Autowired
    private SongService songService;

    @Autowired
    private ListSongService listSongService;

    @Transactional
    @Override
    public boolean deleteById(Integer id) {
        this.removeById(id);
        QueryWrapper<ListSong> listSongQueryWrapper = new QueryWrapper<>();
        listSongQueryWrapper.eq("song_list_id",id);
        List<ListSong> list = listSongService.list(listSongQueryWrapper);
        for (ListSong listSong : list) {
            songService.deleteById(listSong.getSongId());
        }
        return true;
    }

    @Override
    public Map<String, Object> page(Integer pageNum, Integer size, SongListVo songListVo) {
        List<Integer> songIds = new ArrayList<>();

        Page<SongList> songPage = new Page<>(pageNum,size);
        QueryWrapper<SongList> songQueryWrapper = new QueryWrapper<>();

        if (songListVo.getTitle()!=null){
            songQueryWrapper.like("title",songListVo.getTitle());
        }

        this.page(songPage,songQueryWrapper);
        Map<String,Object> data = new HashMap<>();
        data.put("total",songPage.getTotal());
        List<SongList> songList = songPage.getRecords();
        data.put("list",songList);
        return data;
    }
}
